package com.hotel_management_backstage.lin.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.hotel_management_backstage.lin.entity.SysResident;
import com.hotel_management_backstage.lin.entity.SysRoom;
import com.hotel_management_backstage.lin.entity.SysRoomStatus;
import com.hotel_management_backstage.lin.result.RespBean;
import com.hotel_management_backstage.lin.service.SysResidentRoomService;
import com.hotel_management_backstage.lin.service.SysResidentService;
import com.hotel_management_backstage.lin.service.SysRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@RestController
public class RoomController {
    @Autowired
    private SysResidentRoomService sysResidentRoomService;

    @Autowired
    private SysRoomService sysRoomService;

    @PostMapping("api/insertRoom")
    public RespBean insertRoom(@RequestBody SysRoom sysRoom){
        sysRoomService.insertRoom(sysRoom);
        return RespBean.success("操作成功");
    }

    @PostMapping("api/updateRoom")
    public RespBean updateRoom(@RequestBody SysRoom sysRoom){
        sysRoomService.updateRoom(sysRoom);
        return RespBean.success("操作成功");
    }

    @GetMapping("api/allFreeRoom")
    public RespBean getAllFreeRoomNumber(){
        return RespBean.success("",sysRoomService.selectAllFreeRoom());
    }

    //只获取房间本身信息
    @GetMapping("api/allRoom")
    public RespBean getAllRoom(){
        return RespBean.success("",sysRoomService.selectAll());
    }

    /*******/

    @GetMapping("api/allRoomStatus")
    public RespBean getAllRoomStatus(){
        return RespBean.success("",sysResidentRoomService.selectAllRoomStatus());
    }

    @PostMapping("api/insertRoomStatus")
    public RespBean insertRoomStatus(@RequestBody SysRoomStatus sysRoomStatus){
        sysResidentRoomService.insertRoomStatus(sysRoomStatus);
        return RespBean.success("操作成功");
    }

    @PostMapping("api/deleteRoomStatus")
    public RespBean deleteRoomStatus(HttpServletRequest req) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String s = null;
        while((s=br.readLine()) != null){
            sb.append(s);
        }
        JSONObject jsonObject = JSONObject.parseObject(sb.toString());
        Integer roomNumber = jsonObject.getInteger("roomNumber");
        sysResidentRoomService.deleteRoomStatusByRoomNumber(roomNumber);
        return RespBean.success("操作成功");
    }

    @PostMapping("api/deleteRoom")
    public RespBean deleteRoom(HttpServletRequest req) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String s = null;
        while((s=br.readLine()) != null){
            sb.append(s);
        }
        JSONObject jsonObject = JSONObject.parseObject(sb.toString());
        Integer roomNumber = jsonObject.getInteger("roomNumber");
        int i = sysRoomService.deleteRoomByRoomNumber(roomNumber);
        if(i == 1){
            return RespBean.success("操作成功");
        }else{
            return RespBean.error("房间正在使用中！无法删除！");
        }
    }

    @PostMapping("api/updateRoomStatus")
    public RespBean updateRoomStatus(@RequestBody SysRoomStatus sysRoomStatus){
        sysResidentRoomService.updateRoomStatusByRoomNumber(sysRoomStatus);
        return RespBean.success("操作成功");
    }
}
